package com.linkhub.oms.admin.modules.stock.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.stock.domain.param.StockFlowQueryParam;
import com.linkhub.oms.admin.modules.stock.domain.param.StockQueryParam;
import com.linkhub.oms.admin.modules.stock.domain.vo.StockVO;
import com.linkhub.oms.admin.modules.stock.service.StockBizService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.enums.ResponseCodeEnum;
import com.linkhub.oms.common.enums.mp.RoleCodeEnum;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: StockController
 * @Author: echo
 * @Date: 2024/4/25 18:13
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"本地商品库存管理模块"})
@RequestMapping("/stock")
@SaCheckLogin
public class StockController {

    @Resource
    private StockBizService stockBizService;

    @GetMapping("/list")
    @ApiOperation(value = "查询商品库存列表接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<PageDTO<StockVO>> pageStocks(@Validated StockQueryParam stockQueryParam) {
        List<String> userRoles = LhSessionContext.getUserRoles();
        stockQueryParam.setCustomerRole(userRoles.contains(RoleCodeEnum.CUSTOMER.getCode()));
        return DataResponse.success(stockBizService.pageStocks(stockQueryParam));
    }


    @GetMapping("/pickup")
    @ApiOperation(value = "获取提货流水记录")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<List<Object>> pickUpList(@RequestParam("pickUpNo") String pickUpNo) {
        if (StringUtils.isBlank(pickUpNo)) {
            return DataResponse.fail(ResponseCodeEnum.PARAMS_ERROR);
        }
        return DataResponse.success(stockBizService.pickUpRecords(pickUpNo));
    }
}
